package cn.hhchat.wp.entity;

import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.Table;

import cn.hhchat.wp.dto.attachment.SimpleAttachment;
import cn.hhchat.wp.util.DateUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.*;
import org.hibernate.annotations.CascadeType;

import java.util.Date;

/**
 * Created this one by HMH on 2017/10/22.
 */
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "attachment_entity")
public class AttachmentEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;

    String filePath;

    String fileUrl;

    String fileName;

    @CreationTimestamp
    Date createTime;

    @JsonIgnore
    public SimpleAttachment toSimpleAttachment(String urlRoot) {
        SimpleAttachment simpleAttachment = new SimpleAttachment();
        simpleAttachment.setId(this.id);
        simpleAttachment.setCreateTime(DateUtil.formatDateTime(this.createTime));
        simpleAttachment.setUrl(urlRoot + this.fileUrl);
        simpleAttachment.setFileName(this.fileName);
        return simpleAttachment;
    }
}
